@page "/"
@using LiteDB



<PageTitle>Index</PageTitle>

<Table TItem="Foo" OnQueryAsync="OnQueryAsync" OnSaveAsync="OnSaveAsync" ShowToolbar="true" ShowExtendButtons="true" ShowDefaultButtons="true" EditMode="EditMode.InCell">
    <TableColumns>
        <TableColumn @bind-Field="@context.Name"></TableColumn>
    </TableColumns>
</Table>

@code {

    private static LiteDatabase _liteDatabase;
    
    public class Foo
    {
        public int Id { get; set; }
        
        public string Name { get; set; }
    }
    
    private List<Foo> Foos { get; set; }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        if (_liteDatabase == null)
        {
            _liteDatabase = new LiteDatabase("db.db");
        }
        
        Foos = new List<Foo>()
        {
            new Foo()
            {
                Name = "a"
            },
            new Foo()
            {
                Name = "b"
            }
        };
        var collection = _liteDatabase.GetCollection<Foo>("foo");
        collection.Insert(Foos);
    }

    private Task<QueryData<Foo>> OnQueryAsync(QueryPageOptions arg)
    {
        var collection = _liteDatabase.GetCollection<Foo>("foo");
        var queryData = new QueryData<Foo>
        {
            Items = collection.FindAll()
        };
        return Task.FromResult(queryData);
    }

    private Task<bool> OnSaveAsync(Foo arg1, ItemChangedType arg2)
    {
        var collection = _liteDatabase.GetCollection<Foo>("foo");
        collection.Upsert(arg1);
        return Task.FromResult(true);
    }

}